defmodule Explorer.BloomFilterTest do
  use Explorer.DataCase

  alias Explorer.BloomFilter

  describe "Test bloom filter for random Goerli transactions" do
    # {"jsonrpc":"2.0","id": 0,"method":"eth_getTransactionReceipt","params":["0xFE524295C6C01AB25645035A228387BF0E64C8AF429F3DD9D6EF2E3B05337839"]}
    test "#1 (0xFE524295C6C01AB25645035A228387BF0E64C8AF429F3DD9D6EF2E3B05337839)" do
      log_1 =
        insert(:log,
          first_topic: "0xb3813568d9991fc951961fcb4c784893574240a28925604d09fc577c55bb7c32",
          second_topic: "0x000000000000000000000000e38ecdf3cfbaf5cf347e6a3d6490eb34e3a0119d",
          third_topic: "0x000000000000000000000000e38ecdf3cfbaf5cf347e6a3d6490eb34e3a0119d",
          fourth_topic: "0x0000000000000000000000000000000000000000000000000000000000000000",
          address_hash: "0xe93c8cd0d409341205a592f8c4ac1a5fe5585cfa",
          address: nil
        )

      assert BloomFilter.logs_bloom([log_1]) |> Base.encode16(case: :lower) ==
               "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000040000000000000000000000000002000000000000000000000000000000000000000000000000030000000000000000000800000000000000000000000000000000000000000000000002000000008000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000002000000000000000080000000000000000000000"
    end

    test "#2 (0x9f44d7080354147fc42ee0eb62c8f77d0477e7686d18debcb81f90b0d54ea1d1)" do
      log_1 =
        insert(:log,
          first_topic: "0x9866f8ddfe70bb512b2f2b28b49d4017c43f7ba775f1a20c61c13eea8cdac111",
          second_topic: nil,
          third_topic: nil,
          fourth_topic: nil,
          address_hash: "0xd5c325d183c592c94998000c5e0eed9e6655c020",
          address: nil
        )

      log_2 =
        insert(:log,
          first_topic: "0xd342ddf7a308dec111745b00315c14b7efb2bdae570a6856e088ed0c65a3576c",
          second_topic: nil,
          third_topic: nil,
          fourth_topic: nil,
          address_hash: "0xd5c325d183c592c94998000c5e0eed9e6655c020",
          address: nil
        )

      assert BloomFilter.logs_bloom([log_1, log_2]) |> Base.encode16(case: :lower) ==
               "00000000010002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000800000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000020000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000"
    end

    test "#3 (0x2548b6514211bafdfeff37dc184c4700c8ca7056ac2a119bef5a98f8a79662cc)" do
      log_1 =
        insert(:log,
          first_topic: "0x1a37b94876a9c4d5697c33a6fc124022beba6ce60e84469f41d49536d2a55924",
          second_topic: "0x000000000000000000000000000000000000000000000000000000000001ba63",
          third_topic: "0x00000000000000000000000000000000000000000000000000f8b0a10e470000",
          fourth_topic: "0x0000000000000000000000000000000000000000000000000000000000000002",
          address_hash: "0x2a5ccc8813d89119263b49f567c541e925c75f13",
          address: nil
        )

      assert BloomFilter.logs_bloom([log_1]) |> Base.encode16(case: :lower) ==
               "04000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000420000000000000000000000800000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000020000000000000000000000000000000100000000000100000000000002010000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000010000000000000"
    end
  end
end
